
capture log close 

set matsize 10000


log using "LogFiles/WarsAndProsthesisTraitsEstimatesAndPValuesFullBoom.log", replace


**** Initiate Civil War Analysis
*** Assemble data
use "Data/control_classes_CW.dta", clear
append using "Data/control_classes_WWI.dta"
merge 1:1 patnum using "Data/all_patents_basicinfo.dta"
keep if _merge == 3
drop _merge
merge 1:1 patnum using "Data/control_classes_KWSearch.dta"
keep if _merge == 3
drop _merge

tab iyear if nclassgoogle1 == 623

rename iyear year

gen usertraits = (comfort + appearance + appliances)/3  
gen production = (adjustability + simplicity + cost)/3 
gen anyproduction = max(adjustability, simplicity, cost, materials) 

merge m:1 main_uspto using "Data/with_header_subclass_formerge.dta"
tab _merge
drop if _merge == 1
drop if _merge == 2
drop _merge


gen patentcount = 1
gen prosthetics = nclassgoogle1 == 623  
gen othermedicalclass = 0
replace othermedicalclass = 1 if inlist(nclassgoogle1, 424, 514, 128, 600, 601, 602, 604, 606, 607, 435, 800, 351, 433)

*** Generate a numeric variable for the individual war episodes
gen warepisode = .
replace warepisode = 1 if year >= 1840 & year <= 1890
replace warepisode = 2 if year >= 1890 & year <= 1940


gen period = .
replace period = 0 if year >= 1855 & year <= 1861
replace period = 1 if year >= 1862 & year <= 1866

replace period = 0 if year >= 1910 & year <= 1915
replace period = 1 if year >= 1916 & year <= 1922



sum patentcount
sum patentcount if nclassgoogle1 == 623  
sum patentcount if othermedicalclass == 1  
sum patentcount if othermedicalclass == 0 & nclassgoogle1 != 623

capture eststo clear
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials  ///
	if warepisode == 1 & prosthetics == 1, columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials  ///
	if warepisode == 1 & prosthetics == 0, columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials  ///
	if warepisode == 2 & prosthetics == 1, columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials  ///
	if warepisode == 2 & prosthetics == 0, columns(statistics) statistics(mean)

// Appendix Table D3
esttab using "Tables/FullSampleSummaryStatsAll.tex", replace main(mean) aux(sd) label nomtitle nolegend nonotes compress



**** Code for basic generating distribution of class-specific simple differences tabulation tables on the full sample of control classes
preserve 

drop if period == .

bysort warepisode period: egen numyears=nvals(year)
collapse (sum) patentcount (mean) numyears appearance comfort simplicity cost adjustability materials appliances durability production usertraits, by(nclassgoogle1 period warepisode)
gen patentsperyear = patentcount/numyears 
drop if patentsperyear <= 1
drop if period == 2
drop if period == .

** Balance the class-by-episode panel
egen class_by_episode = group(nclassgoogle1 warepisode)
egen class_by_episodecount = count(class_by_episode), by(class_by_episode)
tab class_by_episodecount

drop if class_by_episodecount != 2
drop numyears patentsperyear 

reshape wide patentcount appearance comfort simplicity cost adjustability materials appliances durability production usertraits , i(nclassgoogle1 warepisode) j(period) 

foreach trait in appearance comfort simplicity cost adjustability materials appliances durability production usertraits {
	gen `trait'change = `trait'1 - `trait'0
}
// Generates columns (1) and (3) of table 5
save "Data/simplediffresults/simplediffanalysisdata.dta", replace
foreach trait in appearance comfort simplicity cost adjustability materials appliances durability production usertraits {

	use "Data/simplediffresults/simplediffanalysisdata.dta", clear
	keep if warepisode == 1
	keep nclassgoogle1 `trait'change warepisode 
	gen simpdiffestimate = `trait'change
	gen negativeestimateprostheticsB = 0 
	replace negativeestimateprostheticsB = 1 if `trait'change < 0 & nclassgoogle1 == 623
	egen negativeestimateprosthetics = max(negativeestimateprostheticsB) 
	replace `trait'change = -1*`trait'change if negativeestimateprosthetics == 1
	egen rank = rank(`trait'change)
	egen maxrank = max(rank)
	gen onesidedpvalue = (1 - rank/maxrank) if nclassgoogle1 == 623 
	gen twosidedpvalue = 2*(1 - rank/maxrank) if nclassgoogle1 == 623 
	save "Data/simplediffresults/simplediffpvaluesCIVWAR`trait'.dta", replace
	keep if nclassgoogle1 == 623
	gen trait = "`trait'"
	drop `trait'change
	save "Data/simplediffresults/simplediffpvaluesCIVWAR`trait'tableoutput.dta", replace

	use "Data/simplediffresults/simplediffanalysisdata.dta", clear
	keep if warepisode == 2
	keep nclassgoogle1 `trait'change warepisode
	gen simpdiffestimate = `trait'change
	gen negativeestimateprostheticsB = 0 
	replace negativeestimateprostheticsB = 1 if `trait'change < 0 & nclassgoogle1 == 623
	egen negativeestimateprosthetics = max(negativeestimateprostheticsB) 
	replace `trait'change = -1*`trait'change if negativeestimateprosthetics == 1
	egen rank = rank(`trait'change)
	egen maxrank = max(rank)
	gen onesidedpvalue = (1 - rank/maxrank) if nclassgoogle1 == 623 
	gen twosidedpvalue = 2*(1 - rank/maxrank) if nclassgoogle1 == 623 
	save "Data/simplediffresults/simplediffpvaluesWWI`trait'.dta", replace
	keep if nclassgoogle1 == 623
	gen trait = "`trait'"
	drop `trait'change
	save "Data/simplediffresults/simplediffpvaluesWWI`trait'tableoutput.dta", replace
}

use "Data/simplediffresults/simplediffpvaluesCIVWARcomforttableoutput.dta", clear
foreach trait in appearance appliances adjustability simplicity cost materials durability production usertraits {
	append using "Data/simplediffresults/simplediffpvaluesCIVWAR`trait'tableoutput.dta"
}
foreach trait in comfort appearance appliances adjustability simplicity cost materials durability production usertraits {
	append using "Data/simplediffresults/simplediffpvaluesWWI`trait'tableoutput.dta"
}
save "Data/simplediffresults/simplediffpvaluesfulltableoutput.dta", replace

restore





capture eststo clear
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials  ///
	if warepisode == 1 & period == 0 & prosthetics == 1, columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials  ///
	if warepisode == 1 & period == 1 & prosthetics == 1, columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials  ///
	if warepisode == 1 & period == 0 & prosthetics == 0, columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials  ///
	if warepisode == 1 & period == 1 & prosthetics == 0, columns(statistics) statistics(mean)

esttab using "Tables/SummaryStatsByWarAndTreatment.tex", replace main(mean) aux(sd) label nomtitle nolegend nonotes compress

capture eststo clear
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials  ///
	if warepisode == 2 & period == 0 & prosthetics == 1, columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials  ///
	if warepisode == 2 & period == 1 & prosthetics == 1, columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials  ///
	if warepisode == 2 & period == 0 & prosthetics == 0, columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials  ///
	if warepisode == 2 & period == 1 & prosthetics == 0, columns(statistics) statistics(mean)

esttab using "Tables/SummaryStatsByWarAndTreatment.tex", append main(mean) aux(sd) label nomtitle nolegend nonotes compress


*** Code for basic DD tabulation tables on the full sample of control classes
preserve 

drop if period == .
collapse (sum) patentcount (mean) appearance comfort simplicity cost adjustability materials appliances durability production usertraits  , by(prosthetics period warepisode)
reshape wide patentcount appearance comfort simplicity cost adjustability materials appliances durability production usertraits , i(prosthetics warepisode) j(period) 
reshape wide patentcount* appearance* comfort* simplicity* cost* adjustability* materials* appliances* durability* production* usertraits*, i(warepisode) j(prosthetics) 

foreach trait in appearance comfort simplicity cost adjustability materials appliances durability production usertraits {
	gen `trait'changeprosth = `trait'11 - `trait'01
	gen `trait'changeother = `trait'10 - `trait'00
	gen `trait'diffdiff = `trait'changeprosth - `trait'changeother
}

reshape long patentcount appearance comfort simplicity cost adjustability materials appliances durability production usertraits , i(warepisode)  j(event) string

foreach trait in appearance comfort simplicity cost adjustability materials appliances durability production usertraits {
	label var `trait' "`trait'"
}

// Generates Appendix Table D.6
capture eststo clear
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 1 & event == "01", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 1 & event == "11" , columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 1 & event == "00", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 1 & event == "10", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 1 & event == "changeprosth", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 1 & event == "changeother", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 1 & event == "diffdiff", columns(statistics) statistics(mean)

esttab using "Tables/CollSummaryStatsByWarAndTreatment.tex", replace main(mean) aux(sd) label nomtitle nolegend nonotes compress

capture eststo clear
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 2 & event == "01", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 2 & event == "11" , columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 2 & event == "00", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 2 & event == "10", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 2 & event == "changeprosth", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 2 & event == "changeother", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 2 & event == "diffdiff", columns(statistics) statistics(mean)

esttab using "Tables/CollSummaryStatsByWarAndTreatment.tex", append main(mean) aux(sd) label nomtitle nolegend nonotes compress

restore




preserve 
// Generates Appendix Table D.7

gen medicalclass = 0
replace medicalclass = 1 if inlist(nclassgoogle1, 424, 514, 128, 600, 601, 602, 604, 606, 607, 435, 800, 351, 433, 623)
keep if medicalclass == 1
drop if period == .
collapse (sum) patentcount (mean) appearance comfort simplicity cost adjustability materials appliances durability production usertraits  , by(prosthetics period warepisode)
reshape wide patentcount appearance comfort simplicity cost adjustability materials appliances durability production usertraits , i(prosthetics warepisode) j(period) 
reshape wide patentcount* appearance* comfort* simplicity* cost* adjustability* materials* appliances* durability* production* usertraits*, i(warepisode) j(prosthetics) 

foreach trait in appearance comfort simplicity cost adjustability materials appliances durability production usertraits {
	gen `trait'changeprosth = `trait'11 - `trait'01
	gen `trait'changeother = `trait'10 - `trait'00
	gen `trait'diffdiff = `trait'changeprosth - `trait'changeother
}

reshape long patentcount appearance comfort simplicity cost adjustability materials appliances durability production usertraits , i(warepisode)  j(event) string

foreach trait in appearance comfort simplicity cost adjustability materials appliances durability production usertraits {
	label var `trait' "`trait'"
}

capture eststo clear
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 1 & event == "01", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 1 & event == "11" , columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 1 & event == "00", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 1 & event == "10", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 1 & event == "changeprosth", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 1 & event == "changeother", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 1 & event == "diffdiff", columns(statistics) statistics(mean)

esttab using "Tables/CollSummaryStatsByWarAndTreatmentMED.tex", replace main(mean) aux(sd) label nomtitle nolegend nonotes compress

capture eststo clear
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 2 & event == "01", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 2 & event == "11" , columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 2 & event == "00", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 2 & event == "10", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 2 & event == "changeprosth", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 2 & event == "changeother", columns(statistics) statistics(mean)
eststo: estpost tabstat production usertraits cost simplicity adjustability appliances appearance comfort durability materials   ///
	if warepisode == 2 & event == "diffdiff", columns(statistics) statistics(mean)

esttab using "Tables/CollSummaryStatsByWarAndTreatmentMED.tex", append main(mean) aux(sd) label nomtitle nolegend nonotes compress

restore






drop if year > 1890
*** Civil War Loop
foreach trait in appearance simplicity cost adjustability materials comfort appliances durability production usertraits {

	preserve

	drop if period == .

	collapse (mean) `trait' , by(nclassgoogle1 period)
	reshape wide `trait', i(nclassgoogle1) j(period) 

	drop if `trait'0 == 0
	drop if `trait'0 == .
*	drop if `trait'1 == 0
	drop if `trait'1 == .

	gen estimates`trait' = .
	gen catentry = _n
	sum catentry

	forval i = 1/`r(max)' {

		gen livebaselineB = `trait'0 if catentry == `i'
		egen livebaseline = max(livebaselineB)
		gen livebaselinediffB = `trait'1 - `trait'0 if catentry == `i'
		egen livebaselinediff = max(livebaselinediffB)

		gen nearlivebaseline = 0
		replace nearlivebaseline = 1 if `trait'0 > livebaseline - 0.06 & `trait'0 < livebaseline + 0.06 & catentry != `i'
		egen nearlivebaseline0ave = mean(`trait'0) if nearlivebaseline == 1
		egen nearlivebaseline1ave = mean(`trait'1) if nearlivebaseline == 1
		gen nearlivebaselinediffB = nearlivebaseline1ave - nearlivebaseline0ave
		egen nearlivebaselinediff = max(nearlivebaselinediffB)

		gen estimate = livebaselinediff - nearlivebaselinediff

		replace estimates`trait' = estimate if catentry == `i'

		drop estimate nearlivebaselinediff nearlivebaselinediffB nearlivebaseline1ave nearlivebaseline0ave nearlivebaseline livebaselinediff livebaselinediffB livebaseline livebaselineB 

	}

	
	gen livebaselineB = `trait'0 if nclassgoogle1 == 623
	egen livebaseline = max(livebaselineB)
	gen livebaselinediffB = `trait'1 - `trait'0 if nclassgoogle1 == 623
	egen livebaselinediff = max(livebaselinediffB)

	gen nearlivebaseline = 0
	replace nearlivebaseline = 1 if `trait'0 > livebaseline - 0.06 & `trait'0 < livebaseline + 0.06 & nclassgoogle1 != 623
	egen nearlivebaseline0aveB = mean(`trait'0) if nearlivebaseline == 1
	egen nearlivebaseline1aveB = mean(`trait'1) if nearlivebaseline == 1
	gen nearlivebaselinediffB = nearlivebaseline1aveB - nearlivebaseline0aveB
	egen nearlivebaselinediff = max(nearlivebaselinediffB)
	egen nearlivebaseline0ave = max(nearlivebaseline0aveB)
	egen nearlivebaseline1ave = max(nearlivebaseline1aveB)
	
	xtile ptile = estimates`trait', nq(50)
	replace ptile = ptile*2 - 1
	sum `trait'0 `trait'1 nearlivebaseline0ave nearlivebaseline1ave livebaselinediff nearlivebaselinediff estimates`trait' if nclassgoogle1 == 623
	sum estimates`trait' if nclassgoogle1 == 623
	display "`trait'"
	tab estimates`trait'
	sum estimates`trait'
	sum estimates`trait' if nclassgoogle1 == 623
	sum ptile
	sum ptile if nclassgoogle1 == 623

	keep `trait'0 `trait'1 nearlivebaseline0ave nearlivebaseline1ave livebaselinediff nearlivebaselinediff estimates`trait' nclassgoogle1 
	rename `trait'0 livetreat0
	rename `trait'1 livetreat1
	rename estimates`trait' estimates
	des
	keep if nclassgoogle1 == 623
	gen trait = "`trait'"
	gen warepisode = 1
	save "Data/crudematchresultsdata/crudematchresultsdata`trait'CIV.dta", replace
	
	restore

	
	preserve
	drop if period == .
	collapse (mean) `trait' prosthetics, by(nclassgoogle1 period)

	drop if `trait' == 0
	drop if `trait' == .

	drop if period == .

	gen prostheticsbaselineB = `trait' if nclassgoogle1 == 623 & period == 0
	egen prostheticsbaseline = max(prostheticsbaselineB)

	gen nearprostheticsbaselineB = 0
	replace nearprostheticsbaselineB = 1 if `trait' > prostheticsbaseline - 0.06 & `trait' < prostheticsbaseline + 0.06 & period == 0
	egen nearprostheticsbaseline = max(nearprostheticsbaselineB), by(nclassgoogle1)

	drop if nclassgoogle1 != 623 & nearprostheticsbaseline == 0
	display "`trait'"
	tab nclassgoogle1 

	collapse (mean) `trait' , by(prosthetics period)

	drop if period == .
	label define prostheticslabels 0 "Other" 1 "Prosthetics", replace
	label values prosthetics prostheticslabels, 
	label define periodlabels 0 "Pre War Patents" 1 "Wartime Patents" 2 "Post War", replace
	label values period periodlabels, 
	
	graph hbar `trait' if period != 2, ytitle("Mean emphasis on `trait'", size(small)) over(period, label(labsize(small))) over(prosthetics, label(labsize(small))) graphregion(color(white)) title(Civil War: `trait', color(black)) name(Match`trait'CIVBar, replace)
	graph export "Figures/Match`trait'CIVBar.pdf", replace name(Match`trait'CIVBar)

	restore 

	
}








**** Initiate WWI Analysis
*** Assemble data
use "Data/control_classes_CW.dta", clear
append using "Data/control_classes_WWI.dta"
merge 1:1 patnum using "Data/all_patents_basicinfo.dta"
keep if _merge == 3
drop _merge
merge 1:1 patnum using "Data/control_classes_KWSearch.dta"
keep if _merge == 3
drop _merge

tab iyear if nclassgoogle1 == 623

rename iyear year

gen usertraits = (comfort + appearance + appliances)/3  
gen production = (adjustability + simplicity + cost)/3 
gen anyprodtraits = max(adjustability, simplicity, cost, materials) 

merge m:1 main_uspto using "Data/with_header_subclass_formerge.dta"
tab _merge
drop if _merge == 1
drop if _merge == 2
drop _merge

gen patentcount = 1
gen prosthetics = nclassgoogle1 == 623  



*** World War I Loop
gen period = .
replace period = 0 if year >= 1910 & year <= 1915
replace period = 1 if year >= 1916 & year <= 1922
drop if period == .


foreach trait in appearance simplicity cost adjustability materials comfort appliances durability production usertraits {

	preserve

	drop if period == .
	
	collapse (mean) `trait' , by(nclassgoogle1 period)
	reshape wide `trait', i(nclassgoogle1) j(period) 

	drop if `trait'0 == 0
	drop if `trait'0 == .
*	drop if `trait'1 == 0
	drop if `trait'1 == .

	gen estimates`trait' = .
	gen catentry = _n
	sum catentry

	forval i = 1/`r(max)' {
		gen livebaselineB = `trait'0 if catentry == `i'
		egen livebaseline = max(livebaselineB)
		gen livebaselinediffB = `trait'1 - `trait'0 if catentry == `i'
		egen livebaselinediff = max(livebaselinediffB)

		gen nearlivebaseline = 0
		replace nearlivebaseline = 1 if `trait'0 > livebaseline - 0.06 & `trait'0 < livebaseline + 0.06 & catentry != `i' 
		egen maxnearlivebaseline = max(nearlivebaseline)
		replace nearlivebaseline = 1 if `trait'0 > livebaseline - 0.2 & `trait'0 < livebaseline + 0.2 & catentry != `i' & maxnearlivebaseline == 0
		drop maxnearlivebaseline
		egen nearlivebaseline0ave = mean(`trait'0) if nearlivebaseline == 1
		egen nearlivebaseline1ave = mean(`trait'1) if nearlivebaseline == 1
		gen nearlivebaselinediffB = nearlivebaseline1ave - nearlivebaseline0ave
		egen nearlivebaselinediff = max(nearlivebaselinediffB)

		gen estimate = livebaselinediff - nearlivebaselinediff

		replace estimates`trait' = estimate if catentry == `i'

		drop estimate nearlivebaselinediff nearlivebaselinediffB nearlivebaseline1ave nearlivebaseline0ave nearlivebaseline livebaselinediff livebaselinediffB livebaseline livebaselineB 

	}

	gen livebaselineB = `trait'0 if nclassgoogle1 == 623
	egen livebaseline = max(livebaselineB)
	gen livebaselinediffB = `trait'1 - `trait'0 if nclassgoogle1 == 623
	egen livebaselinediff = max(livebaselinediffB)

	gen nearlivebaseline = 0
	replace nearlivebaseline = 1 if `trait'0 > livebaseline - 0.06 & `trait'0 < livebaseline + 0.06 & nclassgoogle1 != 623 
	egen maxnearlivebaseline = max(nearlivebaseline)
	replace nearlivebaseline = 1 if `trait'0 > livebaseline - 0.2 & `trait'0 < livebaseline + 0.2 & nclassgoogle1 != 623 & maxnearlivebaseline == 0
	drop maxnearlivebaseline
	egen nearlivebaseline0aveB = mean(`trait'0) if nearlivebaseline == 1
	egen nearlivebaseline1aveB = mean(`trait'1) if nearlivebaseline == 1
	gen nearlivebaselinediffB = nearlivebaseline1aveB - nearlivebaseline0aveB
	egen nearlivebaselinediff = max(nearlivebaselinediffB)
	egen nearlivebaseline0ave = max(nearlivebaseline0aveB)
	egen nearlivebaseline1ave = max(nearlivebaseline1aveB)

	xtile ptile = estimates`trait', nq(50)
	replace ptile = ptile*2 - 1
	sum `trait'0 `trait'1 nearlivebaseline0ave nearlivebaseline1ave livebaselinediff nearlivebaselinediff estimates`trait' if nclassgoogle1 == 623
	sum estimates`trait' if nclassgoogle1 == 623
	display "`trait'"
	tab estimates`trait'
	sum estimates`trait'
	sum estimates`trait' if nclassgoogle1 == 623
	sum ptile
	sum ptile if nclassgoogle1 == 623
	
	keep `trait'0 `trait'1 nearlivebaseline0ave nearlivebaseline1ave livebaselinediff nearlivebaselinediff estimates`trait' nclassgoogle1 
	rename `trait'0 livetreat0
	rename `trait'1 livetreat1
	rename estimates`trait' estimates
	des
	keep if nclassgoogle1 == 623
	gen trait = "`trait'"
	gen warepisode = 2
	save "Data/crudematchresultsdata/crudematchresultsdata`trait'WWI.dta", replace

	
	restore

	
	preserve

	collapse (mean) `trait' prosthetics, by(nclassgoogle1 period)

	drop if `trait' == 0
	drop if `trait' == .

	drop if period == .

	gen prostheticsbaselineB = `trait' if nclassgoogle1 == 623 & period == 0
	egen prostheticsbaseline = max(prostheticsbaselineB)

	gen nearprostheticsbaselineB = 0
	replace nearprostheticsbaselineB = 1 if `trait' > prostheticsbaseline - 0.06 & `trait' < prostheticsbaseline + 0.06 & period == 0 & nclassgoogle1 != 623 
	egen maxnearlivebaseline = max(nearprostheticsbaselineB)
	replace nearprostheticsbaselineB = 1 if `trait' > prostheticsbaseline - 0.2 & `trait' < prostheticsbaseline + 0.2 & period == 0 & nclassgoogle1 != 623 & maxnearlivebaseline == 0
	drop maxnearlivebaseline
	egen nearprostheticsbaseline = max(nearprostheticsbaselineB), by(nclassgoogle1)

	drop if nclassgoogle1 != 623 & nearprostheticsbaseline == 0
	display "`trait'"
	tab nclassgoogle1 
	drop if period == .

	collapse (mean) `trait' , by(prosthetics period)

	drop if period == .

	label define prostheticslabels 0 "Other" 1 "Prosthetics", replace
	label values prosthetics prostheticslabels, 
	label define periodlabels 0 "Pre War Patents" 1 "Wartime Patents" 2 "Post War", replace
	label values period periodlabels, 
	
	graph hbar `trait' if period != 2, ytitle("Mean emphasis on `trait'", size(small)) over(period, label(labsize(small))) over(prosthetics, label(labsize(small))) graphregion(color(white)) title(World War I: `trait', color(black)) name(Match`trait'WWIBar, replace)
	graph export "Figures/Match`trait'WWIBar.pdf", replace name(Match`trait'WWIBar)

	restore 


}


**** Append together the data sets for the matching results table
use "Data/crudematchresultsdata/crudematchresultsdataproductionCIV.dta", clear
foreach trait in usertraits cost simplicity adjustability appliances appearance comfort durability  materials {
	append using "Data/crudematchresultsdata/crudematchresultsdata`trait'CIV.dta"
}
foreach trait in production usertraits cost simplicity adjustability appliances appearance comfort durability  materials {
	append using "Data/crudematchresultsdata/crudematchresultsdata`trait'WWI.dta"
}

order trait livetreat0 livetreat1 nearlivebaseline0ave nearlivebaseline1ave livebaselinediff nearlivebaselinediff estimates warepisode nclassgoogle1

format livetreat0 livetreat1 nearlivebaseline0ave nearlivebaseline1ave livebaselinediff nearlivebaselinediff estimates  %9.3g 

list *

// Generates Table D.8
save "Data/crudematchresultsdata/crudematchresultsdataTABLE.dta", replace


capture log close
